from django.db.models import Q
from rest_framework import viewsets
from ERP_system.utils.multiple_destory import MultipleDestoryMixin,MultipleOpenMixin
from ERP_system.utils.pagination import GlobalPagination
from goods_info.models import *
from goods_info.serializer.units_serializer import UnitsSerializer


class UnitsView(viewsets.ModelViewSet,MultipleDestoryMixin,MultipleOpenMixin):

    queryset = UnitsModel.objects.all()

    serializer_class = UnitsSerializer


   # pagination_class = GlobalPagination



    def get_queryset(self):

        if self.action=='list':


            #接受前端传来的json格式的参数
            units_name=self.request.data.get('name',None)
            #因为参数可能没有，造成过滤条件是动态的

            query=Q()

            if units_name:
                query.add(Q(basic_name__contains=units_name),'OR')
            if units_name:
                query.add(Q(backup_name__contains=units_name),'OR')

            return UnitsModel.objects.filter(query).all()

        else:

            return UnitsModel.objects.all()